<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2022/3/16
 */

namespace App\Services;

use App\Models\Info;
use App\Models\InfoCategory;
use App\Models\Links;

/**
 * 友情链接模块服务层
 *
 * Class LinksService
 * @package App\Services
 */
class LinksService
{
    /**
     * 根据条件获取友情链接
     * @param int $type // 类型：1文字 2图片
     * @param int $limit // 0代表不限制
     * @return \Illuminate\Support\Collection
     */
    static function getList($type = 0, $limit = 0)
    {
        $where = [['status',1]];
        if ($type > 0) {
            array_push($where, ['type',$type]);
        }
        $query = Links::where($where)->orderBy('sort','asc')->orderBy('id','desc');
        if($limit > 0) {
            $query = $query->limit($limit);
        }
        $result = $query->get();
        // 封面图片处理
        $cover_image_ids = $query->pluck('logo_image');
        $getCoverImage = AttachmentService::previewList($cover_image_ids);
        $setCoverImage = [];
        collect($getCoverImage)->each(function ($item) use (&$setCoverImage, &$resetData) {
            $setCoverImage[$item['id']] = $item;
        });
        return collect($result)->each(function (&$item) use($setCoverImage) {
            $item['image_url'] = AttachmentService::getDefaultImageUrl();
            if(isset($setCoverImage[$item['logo_image']]['url'])) {
                $item['image_url'] = $setCoverImage[$item['logo_image']]['url'];
            }
            // link
            $item['href_'] = $item->href ? " href =$item->href " : '';
            $item['href_target_'] = $item->href_target ? " target =$item->href_target " : '';
            $item['rel_'] = $item->rel ? " rel =$item->rel " : '';
        });
    }

}
